package com.lesent.model.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class DateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtils.class);
    /**
     * 默认日期格式
     */
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    /**
     * 日期格式 yyyyMMdd
     */
    public static final String DATE_PATTERN_YYYYMMDD = "yyyyMMdd";

    /**
     * 日期格式 yyyyMMddHHmm
     */
    public static final String DATE_PATTERN_YYYYMMDDHHMM = "yyyyMMddHHmm";

    /**
     * 默认时间戳格式，到毫秒 yyyyMMddHHmmssSSS
     */
    public static final String DATEDETAIL_PATTERN_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

    /**
     * 默认时间格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 统计时间格式：yyyy-MM-dd HH:mm:ss
     */
    public static final String TONGJI_DATETIME_PATTERN = "yyyy-MM-dd HH:mm";
    /**
     * 默认时间戳格式，到毫秒 yyyy-MM-dd HH:mm:ss SSS
     */
    public static final String DEFAULT_DATEDETAIL_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";


    /**
     *  秒转换成String类型
     * @param second
     * @param formatType
     * @return
     */
    public static String dateToString(long second,String formatType) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(second * 1000);
        return dateToString(c.getTime(), formatType);
    }

    /**
     *  秒转换成时间
     * @param time
     * @param timeUnit
     * @return
     */
    public static Date longToDate(long time, TimeUnit timeUnit) {
        Calendar c = Calendar.getInstance();
        switch (timeUnit) {
            case DAYS:
                time = time * 1000 * 24 * 60 * 60;
                break;
            case HOURS:
                time = time * 60 * 60 * 1000;
                break;
            case MINUTES:
                time = time * 60 * 1000;
                break;
            case SECONDS:
                time = time * 1000;
                break;
            default:
                break;
        }
        c.setTimeInMillis(time);
        return c.getTime();
    }

    /**
     *  秒转换成String类型
     * @param dateStr
     * @param formatType
     * @return
     */
    public static Date stringToDate(String dateStr,String formatType) throws Exception {
        return new SimpleDateFormat(formatType).parse(dateStr);
    }

    /**
     * 获取当前日期 yyyyMMdd
     *
     * @return
     */
    public static Date getToday() throws Exception {
        return stringToDate(dateToString(new Date(), DEFAULT_DATE_PATTERN), DEFAULT_DATE_PATTERN);
    }

    /**
     * date类型转换为String类型
     * @param date
     * @param formatType
     * @return
     */
    public static String dateToString(Date date, String formatType) {

        return new SimpleDateFormat(formatType).format(date);
    }

    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate() {
        return new SimpleDateFormat(DEFAULT_DATE_PATTERN).format(new Date());
    }


    /**
     * 获取年月日时分秒
     * @return
     */
    public static int getSegmentOfDate(Date date,int field) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(field);
    }

    /**
     * 获取年月日时分秒
     * @return
     */
    public static long differenceByMinute(Date before,Date after) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(before);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(after);
        return (calendar2.getTimeInMillis() - calendar1.getTimeInMillis())/(1000*60);
    }


    public static void main(String[] args) {
        try {
            System.out.println(differenceByMinute(stringToDate("2018-08-03",DEFAULT_DATE_PATTERN),new Date()));
            System.out.println(getToday());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
